﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection.Emit;
using System.Reflection;

namespace Fabs.Net.GIL
{
    public class Function : IFunction
    {
        public Function()
        {
            Attributes = new List<MethodAttributes>();
            Locals = new List<Local>();
            ParameterTypes = new List<Type>();
        }

        public string Name { get; set; }
        public Type ReturnType { get; set; }
        
        public MethodBuilder Output { get; private set; }

        public bool IsEntryPoint { get; private set; }

        public void Compile(Class Parent)
        {
            var parent = Parent.Output;

            MethodAttributes attributes = Attributes[0];
            for (int i = 1; i < Attributes.Count; i++)
            {
                attributes |= Attributes[i];
            }

            if (Name == "Main" && attributes.HasFlag(MethodAttributes.Static))
            {
                IsEntryPoint = true;
            }

            Output = parent.DefineMethod(Name, attributes, ReturnType, ParameterTypes.ToArray());
            Emit(Parent);
        }

        private void Emit(Class Parent)
        {
            // Emit işlemi yapılacak
        }
    }
}